<?php

namespace app\modules\Order\models;

use Yii;
use app\modules\Cruise\models\Room;
use app\modules\Product\models\Product;
use app\modules\Product\models\ProductRoom;
use dektrium\user\models\User;

/**
 * This is the model class for table "order_room".
 *
 * @property integer $id
 * @property integer $order_id
 * @property integer $customer_id
 * @property integer $room_id
 * @property string $price
 * @property string $remark
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class OrderRoom extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_room';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id'], 'required'],
            [['order_id', 'product_id', 'room_id', 'status', 'user_id', 'num', 'final_room_id', 'final_num'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['price', 'commiss_price'], 'number'],
            [['remark'], 'string', 'max' => 255],
        ];
    }


    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
            $this->final_num  = $this->num;
            $this->final_room_id = $this->room_id;
        }
        return parent::beforeSave($insert);
    }//}}}

    // 保存后执行
	public function afterSave($insert, $changedAttributes){//{{{

		parent::afterSave($insert, $changedAttributes);

		if($insert) {
			// 根据舱房编号获取舱房名称 	
			//$name = $this->productRoom->room->name . '  ' . $this->productRoom->room->code ;

			//Yii::$app->logs->info($this->order_id, 1, sprintf("添加新舱房 %s", $name));
		}
	}//}}}


	// 获取对应航期
	public function getProduct(){
	
        return $this->hasOne(Product::className(), ['id' => 'product_id']);
	}

	// 获取对应订单信息
	public function getOrder(){
	
        return $this->hasOne(Order::className(), ['id' => 'order_id']);
	}

    //  获取订单对应舱房信息
    public function getRoom(){
        return $this->hasOne(Room::className(), ['id' => 'room_id'])->onCondition(['status'=>1]);
    }

	// 获取舱房名字
	public function getRoomName(){//{{{

		$order = Order::findOne($this->order_id);

		if($order->product->stock == 1){  // 标准库存格式
        	$room = ProductRoom::findOne($this->room_id);
        	$m = Room::findOne($room->room_id);
        	//$m = $this->hasOne(Room::className(), ['id' => $room->room_id]);
			return $m->name; 
		}elseif($order->product->stock == 2){	// 自定义库存格式

			$room = $order->product->rooms;

			if($room){

				$arr = json_decode($room, true);
				return $arr[$this->room_id]['name'];
			}else{
				return "无法获取";
			}
		}
	}//}}}




    //  获取订单舱房 对应舱房库存设置详细信息
    public function getProductRoom(){
        return $this->hasOne(ProductRoom::className(), ['id' => 'room_id']);
    }

    public function getProductFinalRoom(){
        return $this->hasOne(ProductRoom::className(), ['id' => 'final_room_id']);
    }

    //  获取订单舱房对应的客户信息
    public function getOrderRoomCustomer(){
        return $this->hasMany(OrderRoomCustomer::className(), ['order_room_id' => 'id'])->onCondition(['status'=>1]);
    }

    public function getOrderRoomCustomerapi(){
        return $this->hasMany(OrderRoomCustomer::className(), ['order_room_id' => 'id']);
    }

    //  获取订单舱房对应的客户的人数
    public function getOrderRoomCustomerNum(){
        return $this->hasMany(OrderRoomCustomer::className(), ['order_room_id' => 'id'])->onCondition(['status'=>1])->count();
    }

    //  获取订单负责销售信息
    public function getMember(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }


	// 获取房间拼房的房间
	public function getMergeRoom(){
		return OrderRoom::findOne($this->merge_id);	
	}

	// 获取舱房对应的最大佣金
	public function getMaxCommiss(){//{{{
        //$data = $this->hasMany(ProductRoom::className(), ['id' => 'room_id']);

		$max_price = 0;

		$product = $this->product;	

		if($product->stock == 1){
			
			$product_room = $this->productRoom;
			$max_price = $product_room->commiss_price;

		}elseif($product->stock == 2){

			$t = json_decode($product->rooms, true);

			$max_price = isset($t[$this->room_id]['commiss_price']) ? $t[$this->room_id]['commiss_price'] : 0;
		}

		return $max_price;
	}//}}}

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.order', 'ID'),
            'order_id' => Yii::t('app.order', 'Order ID'),
            'room_id' => Yii::t('app.order', 'Room ID'),
            'remark' => Yii::t('app.order', 'Remark'),
            'status' => Yii::t('app.order', 'Status'),
            'user_id' => Yii::t('app.order', 'User ID'),
            'created_at' => Yii::t('app.order', 'Created At'),
            'updated_at' => Yii::t('app.order', 'Updated At'),
            'num' => Yii::t('app.order', 'Num'),
            'final_room_id' => Yii::t('app.order', 'Final Room Id'),
            'final_num' => Yii::t('app.order', 'Final Num'),
        ];
    }
}
